/**
 * Created by WT-SUN on 2017/4/12.
 */

$(function () {
    $('#btn_save').on("click", function () {
        submitForm();
    });
    /**
     * 绑定监测单位选择事件
     */
    $("#monitorCompanyName").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                openSerViceTb();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $("#monitorCompanyId").val('');
            }
        }]
    });
    var isMonitorData=[{id:'0',text:'是'},{id:'1',text:'否'}];
    $("#isOverproof").combobox({
        data: isMonitorData,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '150',
        selectOnNavigation:true
    });
    $("#isMonitor").combobox({
        data: isMonitorData,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '150',
        selectOnNavigation:true,
        onSelect: function (record) {
            var id = record.id;
            if (id == "1") {
                $('#monitorCompanyName').textbox({
                    required: true
                });
            } else {
                $('#monitorCompanyName').textbox({
                    required: false
                });
            }
        }
    });

});
function openSerViceTb() {
    WindowManager.openLayer({
        type: 2,
        title: '选择服务机构',
        area: ['850px', '70%'],
        content: ROOT_PATH + '/occupationalHealth/hseServiceOrg/orgTb.shtml',
        callBack:function(data){
            getSelectedOrg(data);
        }
    });
}
function getSelectedOrg(data) {
    $("#monitorCompanyName").textbox('setValue', data[0].serviceName).textbox('setText', data[0].serviceName);
    $("#monitorCompanyId").val(data[0].id);
}

function submitForm() {
    $('#formEditMonitor').form('submit', {
        url: ROOT_PATH + '/occupationalHealth/monitor/updateByIdSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000
                }, function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    $.getIframeWindow().reLoadData();
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
